package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.Part;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.service.PartService;
import com.yanyilu.qiubite.util.PageUtil;
import com.yanyilu.qiubite.util.ParseJwtUtil;
import com.yanyilu.qiubite.util.R;
import com.yanyilu.qiubite.util.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("part")
public class PartController {
    @Autowired
    PartService partService;

    @Autowired
    AdminUserService adminUserService;

    /**
     * 角色列表
     * @param storyId
     * @return
     */
    ///qiubite/part/frontend/list
    @GetMapping("frontend/list")
    public R grtList(@RequestParam("storyId")Integer storyId){
        List<Part> list = partService.getParList(storyId);
        return R.ok().put("list",list);
    }

    /**
     * 添加角色
     * @param part
     * @return
     */
    @PostMapping("/admin/add")
    public R addPart(@RequestBody Part part){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role) ){
            Integer userid = ParseJwtUtil.getUserId();
            Integer spotId = adminUserService.findSpotIdByUserId(userid);
            if(null == part.getStoryId()){
                return R.error("请选择故事id");
            }
            part.setSpotId(spotId);
            partService.addPart(part);
            return R.ok();
        }else if("0".equals(role)){
            if(null == part.getStoryId()){
                return R.error("请选择故事id");
            }
            partService.addPart(part);
            return R.ok();
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 角色修改
     * @param part
     * @return
     */
    //qiubite/part/admin/update
    @PostMapping("/admin/update")
    public R updatePart(@RequestBody Part part){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role) || "0".equals(role)){
            partService.addPart(part);
            return R.ok();
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 角色删除
     * @param partId
     * @return
     */
    @PostMapping("/admin/delete")
    public R deletePart(@RequestParam("partId")Integer partId){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role)){
            partService.deletePart(partId);
            return R.ok();
        }else if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            if(partService.containPartIdsByUserId(userid,partId)){
                partService.deletePart(partId);
                return R.ok();
            }else{
                return R.error(ResultCode.NO_ROLE_ERROR);
            }
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 角色列表
     * @param page
     * @param limit
     * @param partName
     * @return
     */
    @GetMapping("/admin/partlist")
    public R getPartList(@RequestParam("page") Integer page,
                         @RequestParam("limit") Integer limit,
                         @RequestParam(value = "partName",required = false)String partName){
        String role = ParseJwtUtil.getRole();
        if("1".equals(role)){
            Integer userid = ParseJwtUtil.getUserId();
            List<Integer> storyIds = adminUserService.findStoryIdByUserId(userid);
            Pageable pageable = PageUtil.pagelimit(page, limit);
            if(null == partName || partName.trim().length() == 0){
                partName = null;
            }
            Page<Part> pages = partService.getListByStoryIds(partName,pageable,storyIds);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else if("0".equals(role)){
            Pageable pageable = PageUtil.pagelimit(page, limit);
            Part part = new Part();
            if(null == partName || partName.trim().length() == 0){
                partName = null;
            }
            part.setPartName(partName);
            ExampleMatcher matcher = ExampleMatcher.matching().withMatcher("partName", ExampleMatcher.GenericPropertyMatchers.contains());
            Example<Part> example = Example.of(part, matcher);

            Page<Part> pages = partService.getList(example, pageable);
            return R.ok().put("list",pages.getContent()).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 管理员获取角色列表
     * @param page
     * @param limit
     * @param storyId
     * @return
     */
    @GetMapping("/admin/list")
    public R findPartList(@RequestParam("page") Integer page,
                         @RequestParam("limit") Integer limit,
                         @RequestParam("storyId")Integer storyId){
        String role = ParseJwtUtil.getRole();
        if("0".equals(role) || "1".equals(role)){
            Pageable pageable = PageUtil.pagelimit(page, limit);
            return partService.getParListByStroyId(storyId,pageable);
        }else {
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }

    /**
     * 获取角色详情
     * @param partId
     * @return
     */
    @GetMapping("/frontend/info")
    public R getInfo(@RequestParam("partId")Integer partId){
        return partService.getPartInfo(partId);
    }

    /**
     * 结束语查询
     * @param partId
     * @return
     */
    @RequestMapping("/frontend/end")
    public R endContent(@RequestParam("partId")Integer partId){
        Integer userId = ParseJwtUtil.getUserId();
        return partService.endContentByPartId(partId,userId);
    }

}
